<template>
  <el-table :data="tableData" show-summary>
    <el-table-column width="100" prop="date" label="日期" />
    <el-table-column width="80" label="课次">
      <template #default="scope">{{scope.row.classIndex.toString()}}</template>
    </el-table-column>
    <el-table-column width="120" label="时段" prop="timeRange" />
    <el-table-column width="80" label="课时" prop="classHour" />
    <el-table-column prop="curriculum" label="课程" />
    <el-table-column width="80" label="理论课时" align="center" prop="theoreticalClassHours" />
    <el-table-column width="80" label="实训课时" align="center" prop="practicalTrainingHours" />
  </el-table>
</template>

<script setup lang="ts">
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
let props = defineProps(["info", "archiveBinding", "type"]);
const { proxy } = getCurrentInstance();

let tableData = ref([]);
let archiveBindingList = [];
let templateUrl = "";

function field_format(index, field) {
  if (!archiveBindingList.length) return;
  return archiveBindingList[index][field];
}

async function getInfo() {
  let resp = await counter.getTeachingPlan(props.info._id);
  console.log("resp.tableData", resp.tableData);
  archiveBindingList = resp.archiveBindingList;
  console.log("archiveBindingList", resp.archiveBindingList);
  resp.tableData.forEach((element, index) => {
    element.classIndex = field_format(index, "classIndex");
    element.timeRange = field_format(index, "timeRange");
    element.classHour = field_format(index, "classHour");
    element.curriculum = field_format(index, "curriculum");
    element.theoreticalClassHours =
      field_format(index, "theoreticalClassHours") * 1;
    element.practicalTrainingHours =
      field_format(index, "practicalTrainingHours") * 1;
  });
  tableData.value = resp.tableData;
}

async function getTemplateUrl() {
  templateUrl = await proxy.$apis.fileTemplate({
    data: { type: props.type },
    type: "downLoad",
  });
}

function downLoadTemplate() {
  window.location.href = templateUrl;
}

async function exportWord() {
  if (!templateUrl) return proxy.$utils.showToast("请先设置模板");
  let url = await proxy.$apis.classDocExport({
    templateURL: templateUrl,
    data: await counter.teachingPlanExportParams(
      tableData.value,
      archiveBindingList,
      props.info._id
    ),
    fileName: "授课计划.docx",
  });
  window.location.href = url;
}

watch(
  () => [props.info, props.archiveBinding],
  (newValue, oldValue) => {
    if (newValue) {
      nextTick(() => {
        getInfo();
        getTemplateUrl();
      });
    }
  },
  { deep: true, immediate: true }
);

defineExpose({ exportWord, downLoadTemplate, getTemplateUrl });
</script>



<style lang="scss" scoped>
::v-deep .el-table__footer-wrapper .cell {
  font-weight: 600;
  color: #000;
}
</style>

